package greedy.总结.基础;

public class leetcode921使括号有效的最少添加 {
    public int minAddToMakeValid(String s) {
        int left = 0;
        int count = 0;
        int length = s.length();
        for (int i = 0; i < length; i++) {
            if (s.charAt(i) == '(') {
                left++;
            } else {
                if (left <= 0) {
                    count++;
                } else {
                    left--;
                }
            }
        }
        return count + left;
    }
}
